package com.example.perplonews.a_d_fragment.d_live

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.perplonews.R
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.c_video.adapter.Small_Adapter
import com.example.perplonews.a_d_fragment.c_video.entity.Data
import com.example.perplonews.a_d_fragment.c_video.entity.SmallVideo
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response


class LiveFragment : Fragment() {
    private val liveRec1: RecyclerView by lazy { mview!!.findViewById<RecyclerView>(R.id.live_rec_1) }

    private var mview:View?= null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mview = inflater.inflate(R.layout.fragment_live, container, false)
        init()
        return mview
    }

    private fun init() {
        OkGo.get<String>("http://v.juhe.cn/toutiao/index?type=top&key=e3080ff4a57ed090c999dc0d9082dce4").execute(object :
            StringCallback(){
            override fun onSuccess(response: Response<String>) {
                var body = response.body()

                var fromJson = Gson().fromJson(body, SmallVideo::class.java)
                var data = fromJson.result.data
                var adapter = Live_Adapter(data as ArrayList<Data>)
                liveRec1.adapter = adapter
                liveRec1.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
                adapter.setOnItemClickListener { adapter, view, position ->
                    var intent = Intent(activity,Live_detailsActivity::class.java)
                    intent.putExtra("postion",""+position)
                    startActivity(intent)

                }
                var inflate1 = LayoutInflater.from(context).inflate(R.layout.live_top_1, null)
                var inflate2 = LayoutInflater.from(context).inflate(R.layout.live_top_2, null)
                var inflate3 = LayoutInflater.from(context).inflate(R.layout.live_top_3, null)
                var inflate4 = LayoutInflater.from(context).inflate(R.layout.live_top4, null)




                adapter.addHeaderView(inflate1)
                adapter.addHeaderView(inflate2)
                adapter.addHeaderView(inflate3)
                adapter.addHeaderView(inflate4)
            }

        })
    }


}